package com.ruoyi.bole.service.impl;

import java.util.List;

import com.ruoyi.bole.domain.BoleContent;
import com.ruoyi.bole.service.IBoleContentService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bole.mapper.BoleEventRegistrationMapper;
import com.ruoyi.bole.domain.BoleEventRegistration;
import com.ruoyi.bole.service.IBoleEventRegistrationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动报名Service业务层处理
 * 
 * @author WuJiale
 * @date 2025-03-23
 */
@Service
public class BoleEventRegistrationServiceImpl implements IBoleEventRegistrationService 
{
    @Autowired
    private BoleEventRegistrationMapper boleEventRegistrationMapper;

    @Autowired
    private IBoleContentService boleContentService;
    @Autowired
    private ISysUserService userService;

    @Transactional
    @Override
    public int cancleRegiste(Long regId) {
        BoleEventRegistration boleEventRegistration= boleEventRegistrationMapper.selectBoleEventRegistrationByRegId(regId);
        BoleContent boleContent = boleContentService.selectBoleContentByContentId(boleEventRegistration.getContentId());
        //获取报名积分
        Integer  requiredIntegral = boleContent.getRequiredIntegral();
        System.out.println(requiredIntegral);
        //把积分还给他
//        SysUser sysUser= userService.selectUserById(SecurityUtils.getUserId());
       userService.addScore(SecurityUtils.getUserId(), requiredIntegral);
        return boleEventRegistrationMapper.deleteBoleEventRegistrationByRegId(regId);
    }

    @Transactional
    @Override
    public int check(BoleEventRegistration boleEventRegistration) {


            return boleEventRegistrationMapper.updateBoleEventRegistration( boleEventRegistration);

    }

    @Transactional
    @Override
    public int userRegistration(BoleEventRegistration boleEventRegistration) {
        //先根据contentId查询该活动报名所需积分
        BoleContent boleContent = boleContentService.selectBoleContentByContentId(boleEventRegistration.getContentId());
        //查询用户拥有的积分
        SysUser user = userService.selectUserById(SecurityUtils.getUserId());
        //判断积分是否足够
         if(boleContent.getRequiredIntegral()<= user.getScore()){
             System.out.println("here");
             //够就报名 积分扣掉
             userService.updateScore(user.getUserId().toString(),boleContent.getRequiredIntegral());
             boleEventRegistration.setCreateTime(DateUtils.getNowDate());
             return boleEventRegistrationMapper.insertBoleEventRegistration(boleEventRegistration);
         }else if (boleContent.getRequiredIntegral() ==null){
             System.out.println("null");
             return boleEventRegistrationMapper.insertBoleEventRegistration(boleEventRegistration);
         }else {
             return 0;
         }

    }

    @Override
    public BoleEventRegistration selectByContentIdAndUserId(Long contentId, Long userId) {
        return boleEventRegistrationMapper.selectByContentIdAndUserId(contentId,userId);
    }

    /**
     * 查询活动报名
     * 
     * @param regId 活动报名主键
     * @return 活动报名
     */
    @Override
    public BoleEventRegistration selectBoleEventRegistrationByRegId(Long regId)
    {
        return boleEventRegistrationMapper.selectBoleEventRegistrationByRegId(regId);
    }

    /**
     * 查询活动报名列表
     * 
     * @param boleEventRegistration 活动报名
     * @return 活动报名
     */
    @Override
    public List<BoleEventRegistration> selectBoleEventRegistrationList(BoleEventRegistration boleEventRegistration)
    {
        return boleEventRegistrationMapper.selectBoleEventRegistrationList(boleEventRegistration);
    }

    /**
     * 新增活动报名
     * 
     * @param boleEventRegistration 活动报名
     * @return 结果
     */
    @Override
    public int insertBoleEventRegistration(BoleEventRegistration boleEventRegistration)
    {
        boleEventRegistration.setCreateTime(DateUtils.getNowDate());
        return boleEventRegistrationMapper.insertBoleEventRegistration(boleEventRegistration);
    }

    /**
     * 修改活动报名
     * 
     * @param boleEventRegistration 活动报名
     * @return 结果
     */
    @Override
    public int updateBoleEventRegistration(BoleEventRegistration boleEventRegistration)
    {
        boleEventRegistration.setUpdateTime(DateUtils.getNowDate());
        return boleEventRegistrationMapper.updateBoleEventRegistration(boleEventRegistration);
    }

    /**
     * 批量删除活动报名
     * 
     * @param regIds 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteBoleEventRegistrationByRegIds(Long[] regIds)
    {
        return boleEventRegistrationMapper.deleteBoleEventRegistrationByRegIds(regIds);
    }

    /**
     * 删除活动报名信息
     * 
     * @param regId 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteBoleEventRegistrationByRegId(Long regId)
    {
        return boleEventRegistrationMapper.deleteBoleEventRegistrationByRegId(regId);
    }
}
